﻿using PluginBase;

namespace JobTools.Helper
{
    public class LoadPlugInManager
    {
        public static readonly string PluginsDir = FileHelper.GetPathWithSlash(System.Environment.CurrentDirectory, true) + "Plugins\\";
        public static readonly string ConfigsDir = PluginsDir + "Configs\\";
        public static readonly string ResourcesDir = PluginsDir + "Resources\\";

        /// <summary>
        /// 加载PlugIns插件目录下的dll
        /// </summary>
        public static List<UserControlBase> GetPlugIns()
        {
            List<UserControlBase> lUc = new List<UserControlBase>();

            foreach (var dllFile in Directory.GetFiles(PluginsDir))
            {
                FileInfo fi = new FileInfo(dllFile);
                if (!fi.Name.EndsWith(".dll")) continue;

                foreach (var _uc in CreateInstance(fi.FullName))
                {
                    if (_uc != null)
                    {
                        lUc.Add(_uc);
                    }
                }
            }

            return lUc;
        }

        /// <summary>
        /// 根据全名和路径构造对象
        /// </summary>
        /// <param name="sFilePath">程序集路径</param>
        /// <returns></returns>
        public static List<UserControlBase> CreateInstance(string sFilePath, Type hostType = null)
        {
            List<UserControlBase> lUc = new List<UserControlBase>();
            try
            {
                lUc = ReflectionFactory<UserControlBase>.CreateInstance(sFilePath, new string[] { "Uc", "Plugin" }, hostType);
            }
            catch (Exception ex)
            {
                Console.WriteLine("CreateInstance: " + ex.Message);
            }

            return lUc;
        }

    }
}

